#!/usr/bin/python
# -*- coding: utf8
import sys

import cs_api


__author__ = 'mkompan'

cs_api._EC2_CONFIG = 'ec2_guest.py'

if sys.argv[1].lower() == 'list':
    response = cs_api.command(cs_api.EC2Command(command='listVirtualMachines'))
    try:
        for item in cs_api.response_body(response):
            print
            print item['name'], item['displayname'] if 'displayname' in item else None
            print item['id'], item['nic'][0]['ipaddress'], item['state']
    except UnboundLocalError:
        print "there is no VMs"
elif sys.argv[1].lower() == 'stop' and len(sys.argv)==3:
    response = cs_api.command(cs_api.EC2Command(command='stopVirtualMachine', id=sys.argv[2]))
    print response
elif sys.argv[1].lower() == 'start' and len(sys.argv)==3:
    response = cs_api.command(cs_api.EC2Command(command='startVirtualMachine', id=sys.argv[2]))
    print response
elif sys.argv[1].lower() == 'destroy' and len(sys.argv)==3:
    response = cs_api.command(cs_api.EC2Command(command='destroyVirtualMachine', id=sys.argv[2]))
    print response
else:
    print "unknown command"
